package com.jsnkj.basic.adapter

import android.content.Context
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jsnkj.basic.R
import com.jsnkj.basic.bean.EarningsBean


class RewardTaskAdapter(
    val context: Context,
    private val rewardTaskBeans: List<EarningsBean.BToARatiosBean>
) :
    RecyclerView.Adapter<RewardTaskAdapter.HomeViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.rv_item_reward_task, parent, false)
        return HomeViewHolder(view)
    }

    override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
        val item = rewardTaskBeans[position]

        var aValue: Float = item.a_value
        if (aValue < 0.01F) {
            aValue = item.b_value * item.ratio
        }

        holder.tvTitle.text = "累计提现${item.b_value}元"
        holder.tvRemark.text = "徒弟累计提现${item.b_value}元，师傅得${aValue}元"

        val money = "+${aValue}元"
        val spannableString = SpannableString(money)
        spannableString.setSpan(
            AbsoluteSizeSpan(20, true), 0, money.length - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        // 设置“金币”文本的大小
        spannableString.setSpan(
            AbsoluteSizeSpan(15, true),
            money.length - 1,
            money.length,
            Spanned.SPAN_INCLUSIVE_INCLUSIVE
        )

        holder.tvMoney.text = spannableString
    }

    override fun getItemCount(): Int {
        return rewardTaskBeans.size
    }

    class HomeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvTitle: TextView
        var tvRemark: TextView
        var tvMoney: TextView

        init {
            tvMoney = itemView.findViewById<TextView>(R.id.tv_money)
            tvTitle = itemView.findViewById<TextView>(R.id.tv_title)
            tvRemark = itemView.findViewById<TextView>(R.id.tv_remark)
        }
    }
}